<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {useAuthStore} from "@/stores/auth.js";
import CryptoJS from 'crypto-js'
import {ElMessage} from "element-plus";
import Ring from "@/components/ring.vue";
import WaveCard from "@/components/WaveCard.vue";
import BoardScrollCard from "@/components/BoardScrollCard.vue";
import HoverTrackCard from "@/components/HoverTrackCard.vue";
import MarioBrick from "@/components/MarioBrick.vue";
import Stick from "@/components/stick.vue";
import RandomSentence from "@/views/loginView/components/RandomSentence.vue";
import PlayBtn2 from "@/components/PlayBtn2.vue";
import PlayBtn from "@/components/PlayBtn.vue";
import GlitchBtn from "@/components/GlitchBtn.vue";

const router = useRouter()

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

const errorMsg = ref('')
const isFormValid = ref(false)

// 输入验证
const validateInput = () => {
  // 基本验证
  if (loginForm.username && loginForm.password) {
    isFormValid.value = true
    errorMsg.value = ''
  } else {
    isFormValid.value = false
    errorMsg.value = ''
  }
}

// 登录处理
const handleLogin = async () => {
  // 防止XSS攻击
  const xssPattern = /(~|\{|\}|"|'|<|>|\?)/g
  if (xssPattern.test(loginForm.username) || xssPattern.test(loginForm.password)) {
    errorMessage('警告:输入内容包含非法字符');
    return
  }


  try {
    // 对输入进行转义处理
    const safeUsername = encodeURIComponent(loginForm.username)
    const safePassword = encodeURIComponent(loginForm.password)

    const encryptedPassword = CryptoJS.MD5(safePassword).toString()

    // 实际的登录API调用
    console.log('登录请求:', { username: safeUsername, password: safePassword })
    await useAuthStore().login(safeUsername, encryptedPassword )


    // 登录成功并设置cookie，设置过期时间为72小时
    const expires = new Date(Date.now() + 3600 * 1000 * 72).toUTCString()
    document.cookie = `authToken=yourAuthToken; path=/; expires=${expires}`

  } catch (error) {
    // errorMessage('登录失败，请稍后重试');
  }
}

// 错误提示
const errorMessage = (text) => {
  errorMsg.value = text
  setTimeout(() => {
    errorMsg.value = ''
  }, 3000)
}

onMounted(() => {
  if (useAuthStore().token) {
    router.push('/home')
  } else {
    validateInput()
  }
})

</script>


<template>
  <div class="main-wrapper">
    <!-- 登录和两侧 Stick 的主容器 -->
    <div class="login-stick-container">
      <el-row class="login-stick-row">
        <el-col :xs="0" :sm="0" :md="6" :lg="6" :xl="6">
          <stick class="side-stick"></stick>
        </el-col>
        <el-col
            :xs="24" :sm="24" :md="12" :lg="12" :xl="12"
            class="login-col"
        >
          <board-scroll-card>
            <div class="login-container">
              <div class="form-header">

                <h2>用户登录</h2>
                <p>欢迎回来，请登录您的账号</p>
              </div>
              <form @submit.prevent="handleLogin" class="floating-form">
                <div class="input-group">
                  <input id="username" v-model.trim="loginForm.username" type="text" autocomplete="off" @input="validateInput" required />
                  <label for="username">用户名</label>
                  <span class="highlight"></span>
                </div>
                <div class="input-group">
                  <input id="password" v-model.trim="loginForm.password" type="password" autocomplete="off" @input="validateInput" required />
                  <label for="password">密码</label>
                  <span class="highlight"></span>
                </div>
                <div class="error-message" v-if="errorMsg">{{ errorMsg }}</div>
                <button type="submit" class="submit-btn" >
                  <span>登录</span>
                  <i class="arrow-icon"></i>
                </button>
                <div class="form-footer">
                  <span>还没有账号？</span>
                  <a href="/register">立即注册</a>
                </div>
              </form>

            </div>
          </board-scroll-card>
        </el-col>
        <el-col :xs="0" :sm="0" :md="6" :lg="6" :xl="6">
          <stick class="side-stick"></stick>
        </el-col>
      </el-row>
    </div>

    <!-- 随机句子组件 -->
    <div class="sentence-container">
      <random-sentence></random-sentence>
    </div>
  </div>
</template>

<style scoped>
.main-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 98vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 10px;
  box-sizing: border-box;
  overflow: hidden;
}

.login-stick-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  width: 100%;
}

.login-stick-row {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

.login-col {
  display: flex;
  justify-content: center;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}

.sentence-container {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 10px 0 20px;
  box-sizing: border-box;
}

.login-container {
  width: 90vw;
  max-width: 40vw;
  background: white;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  z-index: 1;
  box-sizing: border-box;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h2 {
  color: #2c3e50;
  font-size: clamp(22px, 5vw, 28px);
  margin-bottom: 8px;
  font-weight: 700;
}

.form-header p {
  color: #95a5a6;
  font-size: clamp(13px, 3vw, 14px);
}

.floating-form .input-group {
  position: relative;
  margin-bottom: 25px;
}

.input-group input {
  width: 100%;
  padding: 14px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: transparent;
  box-sizing: border-box;
}

.input-group label {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  background: white;
  padding: 0 5px;
  color: #95a5a6;
  font-size: clamp(14px, 4vw, 16px);
  transition: all 0.3s ease;
  pointer-events: none;
}

.input-group input:focus,
.input-group input:valid {
  border-color: #3498db;
}

.input-group input:focus + label,
.input-group input:valid + label {
  top: 0;
  font-size: 13px;
  color: #3498db;
}

.submit-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(to right, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: clamp(15px, 4vw, 16px);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-sizing: border-box;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
}

.arrow-icon {
  border: solid white;
  border-width: 0 2px 2px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(-45deg);
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  color: #95a5a6;
  font-size: clamp(13px, 3vw, 14px);
}

.form-footer a {
  color: #3498db;
  text-decoration: none;
  margin-left: 5px;
  font-weight: 600;
}

.form-footer a:hover {
  text-decoration: underline;
}

.error-message {
  color: #f56c6c;
  font-size: clamp(13px, 3vw, 14px);
  text-align: center;
  margin-bottom: 15px;
}

/* 移动端特定样式 */
@media (max-width: 767px) {
  .login-container {
    max-width: 85vw;
    padding: 25px 20px;
    border-radius: 12px;
  }

  .input-group input {
    padding: 12px 14px;
  }

  .submit-btn {
    padding: 12px 14px;
  }
}

@media (max-width: 480px) {
  .login-container {
    padding: 20px 15px;
    max-width: 85vw;
  }

  .input-group {
    margin-bottom: 20px;
  }
}

/* 横屏优化 */
@media screen and (max-width: 767px) and (orientation: landscape) {
  .main-wrapper {
    min-height: auto;
    padding-top: 20px;
  }

  .login-container {
    max-width: 85vw;
    padding: 15px;
  }

  .form-header {
    margin-bottom: 15px;
  }

  .floating-form .input-group {
    margin-bottom: 15px;
  }
}

/* 超大字体设备优化 */
@media screen and (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
  .form-header h2 {
    font-size: 28px;
  }

  .input-group input {
    padding: 16px;
    font-size: 18px;
  }

  .submit-btn {
    padding: 16px;
    font-size: 18px;
  }
}
</style>